package com.sugon.modules.base.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.entity.SysMsgEntity;
import com.sugon.modules.base.consts.SysApproveStatusConst;
import com.sugon.modules.base.model.dto.HandleApproveResult;
import com.sugon.modules.base.model.vo.BaseApproveParamVO;
import com.sugon.modules.base.service.IErmsBaseApproveManager;
import com.sugon.modules.sys.consts.ProcessApproveStatus;
import com.sugon.service.SysMsgService;
import com.sugon.service.SysProcessInfoService;
import com.sugon.utils.ProcessInfoEnum;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ： YuXD
 * @description：
 * @date ： 2020-11-03 上午 9:29
 * @version: 1.0
 */
public abstract class AbstractErmsBaseApproveManager implements IErmsBaseApproveManager {

    private static final String PE = "PE";

    private static final String PE2 = "PE2";

    @Resource
    private SysMsgService msgService;
    @Resource
    private SysProcessInfoService processInfoService;

    /**
     * workshopApprove
     *
     * @param approveParam 审核参数
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean workshopApprove(BaseApproveParamVO approveParam) {
        return commonApprove(approveParam, "WORKSHOP");
    }

    /**
     * peApprove
     *
     * @param approveParam 审核参数
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean peApprove(BaseApproveParamVO approveParam) {
        return commonApprove(approveParam, "PE");
    }

    /**
     * pe2Approve
     *
     * @param approveParam 审核参数
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean pe2Approve(BaseApproveParamVO approveParam) {
        return commonApprove(approveParam, "PE2");
    }

    /**
     * commonApprove
     *
     * @param approveParam
     * @param handleDept
     * @return
     */
    private boolean commonApprove(BaseApproveParamVO approveParam, String handleDept) {
        ProcessInfoEnum processInfo = approveParam.getProcessItem();
        Assert.notNull(processInfo, "审批事项类别必填！");
        List<String> approveIdList = approveParam.getApproveIdList();
        // 获取审核结果
        int approveStatus = approveParam.getStatus();
        for (String approveItemId : approveIdList) {
            // 创建公共消息
            SysMsgEntity sysMsg = new SysMsgEntity();
            sysMsg.setDataId(approveItemId);
            sysMsg.setDataUrl(StrUtil.format(processInfo.getApproveUrl(), approveItemId));
            sysMsg.setStartDate(DateUtil.date());
            HandleApproveResult handleApproveResult = null;
            /*
                目前只处理室主任、车间、工厂三级审核;
                如果不特别说明，默认车间审核
             */
            if (PE.equals(handleDept)) {
                handleApproveResult = handlePEApprove(approveItemId, approveStatus, approveParam.getRemark());
            } else if (PE2.equals(handleDept)) {
                handleApproveResult = handlePE2Approve(approveItemId, approveStatus, approveParam.getRemark());
            } else {
                handleApproveResult = handleWorkshopApprove(approveItemId, approveStatus, approveParam.getRemark());
            }
            // 如果仍然为空，说明逻辑可能没有写，那么不做任何处理
            if (handleApproveResult == null) {
                continue;
            }
            sysMsg.setDataFlag(handleApproveResult.getProcessProgressStatus().toString());
            // 声明进度状态
            Integer processStatus = approveStatus;
            // 如果审核通过
            if (approveStatus == SysApproveStatusConst.PASSED) {
                processStatus = ProcessApproveStatus.PASSED_WITH_APPROVE;
            } else if (approveStatus == SysApproveStatusConst.NOT_PASSED) {
                sysMsg.setUserId(handleApproveResult.getUserId());
                sysMsg.setRemark(StrUtil.format(processInfo.getNotPassedMsgTemplate(), handleApproveResult.getTitle(), approveParam.getRemark()));
            }
            // 1、生成流程节点信息
            processInfoService.saveProcessInfo(null, approveItemId, processInfo, processStatus, null, approveParam.getRemark(), handleApproveResult.getProcessProgressStatus());
            // 保存消息
            msgService.addApproveItem(processInfo, sysMsg, handleApproveResult.getNextNodeLevel());
        }
        return true;
    }

}
